package com.quanjiaolian.chat.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.quanjiaolian.bean.ReviewInfo;
import com.quanjiaolian.chat.service.ChatService;
import com.quanjiaolian.chat.serviceimpl.ChatServiceImpl;
import com.quanjiaolian.tools.ResponseToJson;
import com.quanjiaolian.tools.Utils;

@SuppressWarnings("serial")
public class ReviewAction extends HttpServlet {

	/**
	 * 声明Log4j变量
	 */
	public static ResourceBundle resourceBundle = null;
	static Logger logger = Logger.getLogger(ReviewAction.class.getName());
	
	ChatService cs = new ChatServiceImpl();
	Map<String,Object> resultMap = new HashMap<String,Object>();
	/**
	 * 发表评论
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String review_id = request.getParameter("review_id");//回复人userid
		String replay_id = request.getParameter("replay_id");//被回复人userid
		String chat_id = request.getParameter("chat_id");//新鲜事ID
		String replay_name = request.getParameter("replay_name");//被回复人用户名
		String review_text = request.getParameter("review_text");//回复内容
		String user_type = request.getParameter("user_type");//用户类别
		
		ReviewInfo review = new ReviewInfo();
		review.setChat_id(Integer.parseInt(chat_id));
		review.setReview_id(Integer.parseInt(review_id));
		review.setReplay_id(Integer.parseInt(replay_id));
		review.setReplay_name(Utils.convertString(replay_name));
		review.setReview_text(Utils.convertString(review_text));
		review.setUser_type(Integer.parseInt(user_type));
		
		int i = cs.insertReviewMsg(review);
		if(i>0){
			resultMap.put("result","OK");
		}else{
			resultMap.put("result","Failed");
		}
		ResponseToJson.responseOutWithJson(response,resultMap);
	}

	/**
	 * 根据新鲜事ID查询该条新鲜事下的所有评论
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String user_type = request.getParameter("user_type");
		String chat_id = request.getParameter("chat_id");
		String firstSize = request.getParameter("firstSize");
		String maxSize = request.getParameter("maxSize");
		if("0".equals(firstSize)||firstSize == null){
			firstSize = "1";
		}
		if(maxSize == null){
			maxSize = "5";
		}
		try{
			List<Object> list = cs.queryAllReviewByChatId(Integer.parseInt(firstSize), Integer.parseInt(maxSize), Integer.parseInt(user_type), Integer.parseInt(chat_id));
			resultMap.put("result",list);
		}catch (Exception e) {
			resultMap.put("result","Error");
			e.printStackTrace();
		}
		ResponseToJson.responseOutWithJson(response,resultMap);
	}

}
